package com.irun.pm.ui;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.Callable;

import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.irun.pm.R;
import com.irun.pm.controller.DriverInfoControllers;
import com.irun.pm.interfaces.Callback;
import com.irun.pm.mode.DriverInfo;
import com.irun.pm.utils.StringUtil;
import com.irun.pm.utils.Utils;

/***
 * @author huangsm
 * @date 2012-5-26
 * @email huangsanm@gmail.com
 * @version 1.0
 * @description 驾驶员
 */
public class DriverQueryActivity extends BaseActivity implements OnClickListener, OnItemClickListener{

	private EditText etName;
	private EditText etCarNumber;
	private DriverAdapter mAdapter;             //适配器
	private ListView lvDriver;                  //查询列表
	private DriverInfoControllers controller;
	private List<DriverInfo> listDriver = new ArrayList<DriverInfo>(); //数据存储
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.driver_query);
		
		controller = new DriverInfoControllers(this);
		initAllViews();
	}
	
	private void initAllViews(){
		lvDriver = (ListView)findViewById(R.id.dq_lv_driver);
		listDriver = controller.getDriverInfoList(Utils.TYPE_DRIVER_QUERY);
		mAdapter = new DriverAdapter(listDriver);
		lvDriver.setAdapter(mAdapter);
		lvDriver.setOnItemClickListener(this);
		
		etName = (EditText)findViewById(R.id.dq_et_name);
		etCarNumber = (EditText)findViewById(R.id.dq_et_card);
		
		Button query = (Button)findViewById(R.id.dq_btn_query);
		query.setOnClickListener(this);
	}
	
	class DriverAdapter extends BaseAdapter{
		private List<DriverInfo> listFugitives ;
		
		public DriverAdapter(List<DriverInfo> list){
			listFugitives = list;
		}
		
		@Override
		public int getCount() {
			return listFugitives.size();
		}

		@Override
		public Object getItem(int position) {
			return listFugitives.get(position);
		}

		@Override
		public long getItemId(int position) {
			return position;
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			ViewHolder holder = null;
			if(convertView == null){
				convertView = LayoutInflater.from(DriverQueryActivity.this)
						.inflate(R.layout.car_item, null);
				holder = new ViewHolder();
				holder.tvName = (TextView)convertView.findViewById(R.id.tvCardNum);
				holder.tvSex = (TextView)convertView.findViewById(R.id.tvTime);
				holder.tvCard = (TextView)convertView.findViewById(R.id.tvName);
				convertView.setTag(holder);
			}else{
				holder = (ViewHolder) convertView.getTag();
			}
			holder.tvName.setText(listFugitives.get(position).getName());
			holder.tvSex.setText(listFugitives.get(position).getSex());
			holder.tvCard.setText(listFugitives.get(position).getCard());
			return convertView;
		}
		
	}
	
	class ViewHolder{
		TextView tvName;
		TextView tvSex;
		TextView tvCard;
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.dq_btn_query:
			final String name = etName.getText().toString().trim();
			final String cardNumber = etCarNumber.getText().toString().trim();
			
			doProgress("提示", "正在搜索中...", new Callable<String>() {
				@Override
				public String call() throws Exception {
					// TODO Auto-generated method stub
					Thread.sleep(2000);
					return null;
				}
			}, new Callback<String>() {
				@Override
				public void onCallback(String pCallbackValue) {
					// TODO Auto-generated method stub
					if(StringUtil.isEmpty(name) && StringUtil.isEmpty(cardNumber)){
						listDriver = controller.getDriverInfoList(Utils.TYPE_DRIVER_QUERY);
						mAdapter = new DriverAdapter(listDriver);
						lvDriver.setAdapter(mAdapter);
						Toast.makeText(DriverQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
						return;
					}
					listDriver.clear();
					if(!StringUtil.isEmpty(name) && !StringUtil.isEmpty(cardNumber)){
						listDriver = controller.queryDriverInfoByCardNumberAndName(name, cardNumber, Utils.TYPE_DRIVER_QUERY);
					}else if(!StringUtil.isEmpty(name)){  //单个条件姓名
						listDriver = controller.queryDriverInfoByName(name, Utils.TYPE_DRIVER_QUERY);
					}else if(!StringUtil.isEmpty(cardNumber)){
						listDriver = controller.queryDriverInfoByCardNumber(cardNumber, Utils.TYPE_DRIVER_QUERY);
					}
					if(!listDriver.isEmpty()){
						Toast.makeText(DriverQueryActivity.this, "数据库无法连接，只能查询本地数据", Toast.LENGTH_SHORT).show();
					}else{
						Toast.makeText(DriverQueryActivity.this, "查询不到任何数据", Toast.LENGTH_SHORT).show();
					}
					mAdapter = new DriverAdapter(listDriver);
					lvDriver.setAdapter(mAdapter);
				}
			}, null);
			
			break;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
		DriverInfo driverInfo = (DriverInfo)arg0.getItemAtPosition(arg2);
		Intent intent = new Intent(this,DriverDetailsActivity.class);
		intent.putExtra("id", driverInfo.getId());
		startActivity(intent);
	}
	
}
